package com.fausto.learnimprint.config

import io.swagger.annotations.Api
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.env.Environment
import org.springframework.core.env.Profiles
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.service.ApiInfo
import springfox.documentation.service.Contact
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.swagger2.annotations.EnableSwagger2

@Configuration
@EnableSwagger2
class SwaggerConfig {

    @Bean
    fun swaggerSpringMvcPlugin(environment: Environment): Docket {
        //只在开发环境下开启
        val isEnableSwagger = environment.acceptsProfiles(Profiles.of("dev"))

        return Docket(DocumentationType.SWAGGER_2)
            .enable(isEnableSwagger)
            .groupName("BlogServer")
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.withClassAnnotation(Api::class.java))//为所有标注ApiOperation注解的方法生成文档
            .apis(RequestHandlerSelectors.basePackage("com.fausto.learnimprint"))
            .paths(PathSelectors.ant("/**"))
            .build()
    }

    companion object {
        /**
         * 描述信息
         */
        private fun apiInfo(): ApiInfo {
            return ApiInfo(
                "学习印记Api文档",
                "api接口文档",
                "1.0",
                "http://localhost:80",
                Contact("FAN", "http://localhost:80", "1096249048@qq.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                ArrayList()
            )
        }
    }
}